//
//  RHConfigOptionVC.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/3/12.
//

#import "RHConfigOptionVC.h"
#import "RHLocalParamSetVC.h"
#import "RHPolicySettingVC.h"
#import "RHDeviceParaSetVC.h"
#import "RHEndPointTypeModel.h"
#import "RHEndPointModel.h"
#import "SettingsViewController.h"
#import "RHSolarkSettingVC.h"
#import "RHHiSolarSettingVC.h"
#import "RHTools.h"
#import "KWTMGRNSettingVC.h"
#import "KWT233KInvSettingVC.h"

@interface RHConfigOptionVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) NSArray *dataArr;

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) RHEndPointTypeModel *endPointTypeModel;

@property (nonatomic ,strong) RHEndPointModel *subEndPointModel;//小端点型号,具体的逆变器类型 如30k,60K
@end

@implementation RHConfigOptionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = CFTabViewBgColor;
    self.navigationItem.title = @"运行配置".rh_localized;
    [self setupUI];
    
    //一体机,工商储才有逆变器
    if (self.deviceModel.productTypeId != ProductType_HE_BAT) {
        [self getEndPointReration];
        kWeakSelf;
        [self.tableView addHeaderRefresh:^{
            [weakSelf getEndPointReration];
        }];
    }
    
}

- (void)setupUI{
    self.dataArr = @[@{CellIcon:@"device_set_policy",CellTitle:@"策略配置".rh_localized},@{CellIcon:@"device_set_para",CellTitle:@"参数配置".rh_localized}];
  
    
//    self.dataArr = @[@{CellIcon:@"device_set_policy",CellTitle:@"策略配置".rh_localized},
//                     @{CellIcon:@"device_set_para",CellTitle:@"参数配置".rh_localized},
//                     @{CellIcon:@"device_set_chg",CellTitle:@"充放电配置".rh_localized}];
   
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.backgroundColor = CFTabViewBgColor;
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(10, 0, 0, 0));
    }];
    
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    if (@available(iOS 15.0, *)) {
        _tableView.sectionHeaderTopPadding = 0;
    }
    
}

#pragma mark -----1. 获取设备端点关系 -------
- (void)getEndPointReration{
    [QMUITips showLoadingInView:self.view];
    NSString *url = [NSString stringWithFormat:@"%@/%@",H_getDevRelationTypeList,self.deviceModel.deviceId];
    [RHEndPointTypeModel startGetWithCompletionAPIKey:url argument:@{} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if(resultModel.code == 200){
        
            NSArray *typeArr = resultModel.data;
            
            
            [typeArr enumerateObjectsUsingBlock:^(RHEndPointTypeModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                //已经配置了的逆变器类型
                NSArray *allInvType = [RHTools allConfigedInverterType];
                //根据逆变器类型处理
                if ([allInvType containsObject:obj.endpoint]&&obj.nodes.count>0) {
                    self.endPointTypeModel = obj;
                    RHEndPointModel *invEndpoint = [obj.nodes firstObject];
                    self.subEndPointModel = invEndpoint;
                    self.dataArr = @[@{CellIcon:@"device_set_policy",CellTitle:@"策略配置".rh_localized},
                                     @{CellIcon:@"device_set_para",CellTitle:@"参数配置".rh_localized},
                                     @{CellIcon:@"device_set_mode",CellTitle:@"逆变器工作模式配置".rh_localized}];
                    [self.tableView reloadData];
                    
                    
//                    [self getIverterType:obj];
                    *stop = YES;
                }
                
            }];
            
        }
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.tableView endHeaderRefresh];
    }];
    
    
}

#pragma mark ------- 2.获取逆变器类型 为日月元而设---------
-(void)getIverterType:(RHEndPointTypeModel*)endPointTypeModel
{
    if (endPointTypeModel.nodes.count==0) {
        return;
    }
    RHEndPointModel *endpointModel = [endPointTypeModel.nodes firstObject];
    [QMUITips showLoadingInView:self.view];
    [BaseModel startGetWithCompletionAPIKey:H_getParamStatusData argument:@{@"deviceId":ValidNullData(self.deviceModel.deviceId),@"sn":ValidNullData(endpointModel.sn)} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.tableView endHeaderRefresh];
        if(resultModel.code == 200){
            self.deviceModel.inverterType = (DevInverterType)[request.responseJSONObject[@"data"][@"Inv_DevType"] integerValue];
            if (self.deviceModel.inverterType == DevInverterTypeRYY6K||
                self.deviceModel.inverterType == DevInverterTypeRYY12K) {
                self.dataArr = @[@{CellIcon:@"device_set_policy",CellTitle:@"策略配置".rh_localized},
                                 @{CellIcon:@"device_set_para",CellTitle:@"参数配置".rh_localized},
                                 @{CellIcon:@"device_set_mode",CellTitle:@"逆变器工作模式配置".rh_localized}];
                [self.tableView reloadData];
            }

        }
        
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.tableView endHeaderRefresh];
        
    }];
}




#pragma mark - UITableViewDelegate,UITableViewDataSource
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    kWeakSelf;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"optionCell"];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"optionCell"];
    }
    cell.contentView.backgroundColor = CFTabViewBgColor;
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    [bgView rounded:8 width:1 color:kBorderColor];
    [cell.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));
    }];
    UIImageView *icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:self.dataArr[indexPath.row][CellIcon]]];
    [bgView addSubview:icon];
    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.width.height.offset(35);
        make.centerY.mas_offset(0);
    }];
    
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.textColor = kCOLOR_HEX(@"3F4443");
    titleLab.font = kSYSTEMFONT(15);
    titleLab.text = self.dataArr[indexPath.row][CellTitle];
    [bgView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(icon.mas_right).offset(5);
        make.centerY.mas_offset(0);
    }];
    
    UIImageView *indicator = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow"]];
    [bgView addSubview:indicator];
    [indicator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.width.height.offset(15);
        make.centerY.mas_offset(0);
    }];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    if(indexPath.row == 0){

        [QMUITips showError:@"此设备类型不支持".rh_localized];
        return;

        //策略配置
//        RHPolicySettingVC *policyVC = [[RHPolicySettingVC alloc]init];
//        policyVC.devModel = self.deviceModel;
//        [self.navigationController pushViewController:policyVC animated:YES];
    }else if(indexPath.row == 1){
        //参数设置
        RHDeviceParaSetVC *setVC = [[RHDeviceParaSetVC alloc]init];
        setVC.deviceModel = self.deviceModel;
        [self.navigationController pushViewController:setVC animated:YES];
        
    }else{
        if ([self.subEndPointModel.endpoint isEqualToString:InvType_InvMegarevoLv12KW]) {
            KWTMGRNSettingVC *settingVC = [[KWTMGRNSettingVC alloc]initWithDeviceModel:self.deviceModel endpoint:self.subEndPointModel];
            [self.navigationController pushViewController:settingVC animated:YES];
            
        }else if([self.subEndPointModel.endpoint isEqualToString:InvType_PCS_BCS_B_HM_USX2]){
            KWT233KInvSettingVC *settingVC = [[KWT233KInvSettingVC alloc]initWithDeviceModel:self.deviceModel endpoint:self.subEndPointModel];
            [self.navigationController pushViewController:settingVC animated:YES];
            
        }else if ([self.subEndPointModel.endpoint isEqualToString:InvType_Sol_Ark_30K_3P_208V]||
            [self.subEndPointModel.endpoint isEqualToString:InvType_Sol_Ark_60K_3P_480V]) {//61度电 Solark
            self.deviceModel.invType = self.subEndPointModel.endpoint;
            RHSolarkSettingVC *solisVC = [[RHSolarkSettingVC alloc]initWithDeviceModel:self.deviceModel endpoint:self.subEndPointModel];
            [self.navigationController pushViewController:solisVC animated:YES];
        }else if([self.subEndPointModel.endpoint isEqualToString:InvType_InvHISOLAR15KW]){//海索
            RHHiSolarSettingVC *hiSolarVC = [[RHHiSolarSettingVC alloc]initWithDeviceModel:self.deviceModel endpoint:self.subEndPointModel];
            [self.navigationController pushViewController:hiSolarVC animated:YES];
        }else if ([self.subEndPointModel.endpoint isEqualToString:InvType_InvVoltronicLv6KW]||
                  [self.subEndPointModel.endpoint isEqualToString:InvType_InvVoltronicLv12KW]) {
            //逆变器工作模式设置
            SettingsViewController *rryVC = [[SettingsViewController alloc]init];
            rryVC.title = @"工作模式设置".rh_localized;
            rryVC.deviceModel = self.deviceModel;
            rryVC.endPointTypeModel = self.endPointTypeModel;
            [self.navigationController pushViewController:rryVC animated:YES];
        }
        
       
    }
    

}




@end
